package com.my.screenadapter.mService;

import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.os.Messenger;
import android.os.RemoteException;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

import com.my.screenadapter.Base.BaseActivity;
import com.my.screenadapter.R;
import com.my.screenadapter.TestSingleInstance;
import com.my.screenadapter.mService.MyIntentService;
import com.testmodelconnectionapp.HandlerService;

/**
 * Created by zhangyingjie on 2019-10-14
 *
 * @function
 */
public class MyServiceActivity extends BaseActivity {
    private static final String TAG = "OpenkeyHandlerMsgActivi";
    private ServiceConnection conn;
    private ServiceConnection connMyService;
    private boolean bindMyservice;

    @Override
    protected void init() {
        bindOpenkey();

        TestSingleInstance instance = TestSingleInstance.getInstance();
        Log.i(TAG, "onCreate: instance "+instance.hashCode());
    }
    Messenger mServiceMessenger;
    private Handler mHandler  = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            Log.i(TAG, "handleMessage: what "+msg.what);
            Log.i(TAG, "handleMessage: arg1 "+msg.arg1);
            switch (msg.what){
                case 1:


                break;

            }
        }
    };

    private void bindOpenkey() {
        // TODO:重新绑定远程服务
        Intent jsonReceiver = new Intent();
        jsonReceiver.setAction("com.openkeyproduct.OpenkeyService");
        jsonReceiver.setPackage("com.openkeyproduct");
        conn = new ServiceConnection() {
            @Override
            public void onServiceConnected(ComponentName name, IBinder service) {
                Log.i(TAG, "onServiceConnected: ");
                mServiceMessenger = new Messenger(service);

                Messenger ActivityMessenger = new Messenger(mHandler);
                Message obtain = Message.obtain();
                obtain.what = HandlerService.Handler_ActivityMSG;
                obtain.replyTo = ActivityMessenger;

                try {
                    mServiceMessenger.send(obtain);
                } catch (RemoteException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onServiceDisconnected(ComponentName name) {

            }
        };
        boolean bindOpenKey = bindService(jsonReceiver, conn, BIND_AUTO_CREATE);
    }

    @Override
    protected int initLayoutId() {
        return R.layout.activity_openkeyhandlermsg;
    }

    public void openkey(View view) {

        Message obtain = Message.obtain();
        obtain.what=0x001002;
        Bundle bundle = new Bundle();
        bundle.putString("mac","cc680bf39e04");
        obtain.setData(bundle);
        try {
            if (mServiceMessenger != null) {
                mServiceMessenger.send(obtain);
                Log.i(TAG, "open: send success");
            }else {
                bindOpenkey();
            }
        } catch (RemoteException e) {
            mServiceMessenger = null;
            e.printStackTrace();
        }

    }

    public void unbindOpenkeyService(View view) {
        unbindService(conn);
    }

    public void intentService(View view) {
        Intent intent = new Intent(this, MyIntentService.class);
        intent.putExtra("num1","1");
        startService(intent);
    }
    {
        connMyService = new ServiceConnection() {
            @Override
            public void onServiceConnected(ComponentName name, IBinder service) {
                Log.i(TAG, "onServiceConnected: ");
            }

            @Override
            public void onServiceDisconnected(ComponentName name) {
                Log.i(TAG, "onServiceDisconnected: ");
            }

        };
    }

    public void bindMyService(View view) {
        Log.i(TAG, "bindMyService: ");
        bindMyservice = bindService(new Intent(this, MyService.class), connMyService, BIND_AUTO_CREATE);
        Log.i(TAG, "bindMyService: result---> "+bindMyservice);
    }

    public void unbindMyService(View view) {
        Log.i(TAG, "unbindMyService: ");
        if (connMyService != null && bindMyservice) {
        unbindService(connMyService);
        bindMyservice = false;
        }
    }

    public void startMyService(View view) {
        startService(new Intent(this,MyService.class));
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unbindService(conn);
            if (connMyService != null) {
                unbindService(connMyService);
            }
    }
}
